//index.js
//获取应用实例
var cfg = require('../../utils/cfg');
var util = require('../../utils/util');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
var common = require('../../utils/common');
var sendRequest = require('../../utils/requestUtils');

Page({
  data: {
    choseAddressHide: false,
    address: {},
    settle : {},
    array: ['1份', '2份', '3份', '4份', '5份', '6份', '7份', '8份', '9份', '10份', '10份以上'],
    objectArray: [
      {
        id: 0,
        name: '1份'
      },
      {
        id: 1,
        name: '2份'
      },
      {
        id: 2,
        name: '3份'
      },
      {
        id: 3,
        name: '4份'
      },
      {
        id: 4,
        name: '5份'
      },
      {
        id: 5,
        name: '6份'
      },
      {
        id: 6,
        name: '7份'
      },
      {
        id: 7,
        name: '8份'
      },
      {
        id: 8,
        name: '9份'
      },
      {
        id: 9,
        name: '10份'
      },
      {
        id: 10,
        name: '10份以上'
      }
      
    ],
    index: 0,
    remark : ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let bizOrderId = options.bizOrderId;
    that.loadSettleInfo(bizOrderId);
  },

  toPay: function () {
    let that = this;

    if (!that.data.address.detail) {
      util.alert('请选择收获地址');
      return;
    }

    let orderInfo = {
      openId: app.globalData.openId,
      bizOrderId: that.data.settle.bizOrderId,
      realName: that.data.address.username,
      phone: that.data.address.telNumber,
      province: that.data.address.province,
      city: that.data.address.city,
      area: that.data.address.area,
      detail: that.data.address.detail,
      canjuNum: that.data.array[that.data.index],
      remark: that.data.remark
    }

    sendRequest.sendReq(cfg.djurl.toPay, orderInfo, function (result) {
      console.log('>>>' + result.data.paySign);
      console.log('>>>' + result.data.packageValue);
      wx.requestPayment({
        timeStamp: result.data.timeStamp,
        nonceStr: result.data.nonceStr,
        package: result.data.packageValue,
        signType: result.data.signType,
        paySign: result.data.paySign,
        success(res) {
          util.alert('支付成功啦！');
        },
        fail(res) { 
          console.log(res);
          util.alert('支付失败');
        }
      })
    });
   
  },

  toAddress: function () {
    let that = this;
    console.log(">>>>>address");
    wx.chooseAddress({
      success: function (res) {
        console.log(res.userName)
        console.log(res.postalCode)
        console.log(res.provinceName)
        console.log(res.cityName)
        console.log(res.countyName)
        console.log(res.detailInfo)
        console.log(res.nationalCode)
        console.log(res.telNumber)

        that.setData({
          address: {
            username: res.userName,
            telNumber: res.telNumber,
            province: res.provinceName,
            city: res.cityName,
            area: res.countyName,
            detail: res.detailInfo
          },
          choseAddressHide : true
        });
      }
    })
  },

  loadSettleInfo : function(bizOrderId) {
    let that = this;
    sendRequest.sendReq(cfg.djurl.loadSettle, {
      bizOrderId: bizOrderId,
      openId: app.globalData.openId
    }, function (result) {
      that.setData({
        settle: result.data
      });
    });
  },

  canjuNumChange : function(event) {
    let that = this;
    console.log('picker发送选择改变，携带值为', event.detail.value)
    this.setData({
      index: event.detail.value
    })
  },

  remarkHandler : function(event) {
    this.data.remark = event.detail.detail.value;
    console.log(this.data.remark);
  },
  
  tuikuan : function() {
    let that = this;
    sendRequest.sendReq(cfg.djurl.refundOrder, {
      bizOrderId: that.data.settle.bizOrderId,
      openId: app.globalData.openId
    }, function (result) {
      console.log('>>>>退款成功');
    });
  }

  
})